package com.gerryrun.itemscrollapplication

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.widget.RelativeLayout
import com.gerryrun.itemscrollapplication.databinding.QqSwipeLayoutBinding
import kotlin.math.abs

class QQTouchItemView : RelativeLayout {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    val TAG = "QQTouchItemView"
    lateinit var layoutBinding: QqSwipeLayoutBinding

    private fun initView(context: Context) {
        layoutBinding = QqSwipeLayoutBinding.inflate(LayoutInflater.from(context))
        addView(
            layoutBinding.root,
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
        )
    }

    //记录上次点击位置
    private var lastX: Float = 0f
    private var lastY: Float = 0f

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                rememberLastPosition(event)
            }

            MotionEvent.ACTION_MOVE -> {
                var absDistanceX = 0f
                var absDistanceY = 0f

                absDistanceX = getAbsDistanceX(event)
                absDistanceY = getAbsDistanceY(event)

                var shouldInterceptor: Boolean = false
                Log.e(TAG, "initView: onInterceptTouchEvent : 1111111111")
                if (absDistanceY > absDistanceX) {
                    Log.e(TAG, "initView: onInterceptTouchEvent : 22222222222")
                    shouldInterceptor = false
                } else {
                    Log.e(TAG, "initView: onInterceptTouchEvent : 33333333333")
                    shouldInterceptor = true
                }

                rememberLastPosition(event)

                if (shouldInterceptor) return true else super.onInterceptTouchEvent(event)
            }

            MotionEvent.ACTION_UP -> {
                resetLastXY()
            }

            MotionEvent.ACTION_CANCEL -> {
                resetLastXY()
            }
        }

        return super.onInterceptTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.e(TAG, "initView: setOnTouchListener ACTION_DOWN: 0000000000000")
                rememberLastPosition(event)
                return true
            }

            MotionEvent.ACTION_MOVE -> {
                var shouldInterceptor: Boolean = false
                var absDistanceX: Float = 0f
                var absDistanceY: Float = 0f

                absDistanceX = getAbsDistanceX(event)
                absDistanceY = getAbsDistanceY(event)

                Log.e(
                    TAG,
                    "initView: setOnTouchListener ACTION_MOVE: 1111111111 absDistanceX = $absDistanceX absDistanceY = $absDistanceY"
                )

                if (absDistanceY > absDistanceX) {
                    Log.e(TAG, "initView: setOnTouchListener : 22222222222")
                    shouldInterceptor = false
                } else {
                    Log.e(TAG, "initView: setOnTouchListener : 33333333333")

                    //滑动距离
                    val scrollDistance = layoutBinding.title.x + (event.rawX - lastX)

                    val maxScrollDistance: Float =
                        getScreenWidth(context)- layoutBinding.editButton.x

                    Log.e(
                        TAG,
                        "initView: setOnTouchListener : 33333333333 scrollDistance = $scrollDistance\n maxScrollDistance=$maxScrollDistance"
                    )

                    //滑到最大值，不再滑动
                    if (abs(scrollDistance) >= maxScrollDistance) {
                        layoutBinding.title.x = -(maxScrollDistance)
                    } else
                        layoutBinding.title.x =
                                //不能右滑
                            if (scrollDistance >= 0) 0f else scrollDistance

                    shouldInterceptor = true
                }

                //通知父布局不拦截事件
                parent.requestDisallowInterceptTouchEvent(shouldInterceptor)
                rememberLastPosition(event)

                if (shouldInterceptor) return true else super.onTouchEvent(event)
            }

            MotionEvent.ACTION_UP -> {
                Log.e(TAG, "initView: setOnTouchListener ACTION_UP: 444444444444")

                //滑动距离
                val scrollDistance = layoutBinding.title.x + (event.rawX - lastX)

                val maxScrollDistance: Float =
                    getScreenWidth(context)- layoutBinding.editButton.x

                if (abs(scrollDistance) < maxScrollDistance / 3) {
                    layoutBinding.title.x = 0f
                } else {
                    layoutBinding.title.x =
                        -(maxScrollDistance)
                }

                resetLastXY()
            }

            MotionEvent.ACTION_CANCEL -> {
                Log.e(TAG, "initView: setOnTouchListener ACTION_UP: 55555555555")

                resetTitleViewScrollState()
                resetLastXY()
            }
        }
        return super.onTouchEvent(event)
    }

    //记录上个滑动点
    private fun rememberLastPosition(event: MotionEvent) {
        lastX = event.rawX
        lastY = event.rawY
    }

    private fun getAbsDistanceX(event: MotionEvent): Float {
        return abs(event.rawX - lastX) + 5
    }

    private fun getAbsDistanceY(event: MotionEvent): Float {
        return abs(event.rawY - lastY)
    }

    //重置位置信息
    private fun resetLastXY() {
        lastX = 0f
        lastY = 0f
    }

    /**
     * 重置滑动状态
     */
    fun resetTitleViewScrollState() {
        layoutBinding.title.x = 0f
    }


    fun getScreenWidth(context: Context): Int {
        val resources = context.resources
        val dm = resources.displayMetrics
        return dm.widthPixels
    }
}